import {TypeChatJsonTranslator} from "typechat";
export * from './request-model'
export * from './fetch-remote-file'

export class TemplateDialog {
    private state: Partial<any> = {};
    translator: TypeChatJsonTranslator<any>;
    private prompts: {role:"user" | "system" | "assistant";content:string;}[] = [];

    constructor(translator: any) {
        this.translator = translator;
        this.prompts = []
    }
    
    async process(input: string): Promise<string> {
        // 组合上下文提示
        const prompt = `
        用户当前需求: ${input}
        已确定参数: ${JSON.stringify(this.state.params || {})}
        待补充参数: ${this.state?._missing?.join(",") || "无"}
        历史选择: ${this.state.context?.previousChoices?.join(" -> ") || "无"}`;

        // 获取结构化响应
        const result = await this.translator.translate(prompt,this.prompts.slice(-1,30)) as any;

        console.log(result, 'get all result')

        this.prompts.push({
            role: 'assistant',
            content: JSON.stringify(result.data),
        })

        this.prompts.push({
            role: 'user',
            content: input,
        })
        
        // 更新状态
        this.state = { ...this.state, ...result.data };

        console.log(this.state, 'get my result')
        
        // 检查完整性
        // return this.checkCompleteness();
        return JSON.stringify(this.state)
    }
    
    private checkCompleteness(): string {
        // 根据不同类型检查必填参数
        const { templateType, _missing=[] } = this.state;

        if(!templateType) {
            return "请选择一个模板类型";
        }
        
        if (_missing.length > 0) {
            this.state.context = { 
                ...this.state.context,
                missingParams: _missing.map(m => m.name+': '+m.reason), 
            };
            return `请提供以下信息：${_missing.map(m => m.reason).join("\n")}`;
        }
        
        // 完整时返回确认信息
        return `已为您选择【${templateType}】模板，参数：${JSON.stringify(this.state)}`;
    }
}